home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d985.lha / NewIFF / NewIFF39.lha / newiff39 / other / ilbmscan.c < prev    next >
C/C++ Source or Header  |  1993-09-28  |  7KB  |  301 lines

  1. ;/* ilbmscan.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -j73 ilbmscan.c
  3. Blink FROM LIB:c.o,ilbmscan.o TO ilbmscan LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5.  
  6. *
  7. * ilbmscan.c:    Prints the size, aspect, mode, etc. of ILBM's
  8. *        Scans through an IFF file for all ILBM's
  9. *
  10. * Usage: ilbmscan -c        ; For clipboard scanning
  11. *    or  ilbmscan <file>    ; For DOS file scanning
  12. *
  13. * Based on sift.c by by Stuart Ferguson and Leo Schwab
  14. */
  15.  
  16. #include <exec/types.h>
  17. #include <exec/memory.h>
  18. #include <libraries/dos.h>
  19. #include <libraries/iffparse.h>
  20.  
  21. #include <clib/exec_protos.h>
  22. #include <clib/dos_protos.h>
  23. #include <clib/iffparse_protos.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27.  
  28. #ifdef LATTICE
  29. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  30. int chkabort(void) { return(0); }  /* really */
  31. #endif
  32.  
  33. /*  The structure of a FORM ILBM 'BMHD' and 'CAMG' chunks
  34.  *  Such structures are defined in the spec for a FORM
  35.  *  and may also be provided in include files
  36.  */
  37. /*  Bitmap header (BMHD) structure  */
  38. typedef struct {
  39.         UWORD   w, h;           /*  Width, height in pixels */
  40.         WORD    x, y;           /*  x, y position for this bitmap  */
  41.         UBYTE   nplanes;        /*  # of planes  */
  42.         UBYTE   Masking;
  43.         UBYTE   Compression;
  44.         UBYTE   pad1;
  45.         UWORD   TransparentColor;
  46.         UBYTE   XAspect, YAspect;
  47.         WORD    PageWidth, PageHeight;
  48. } BitMapHeader;
  49.  
  50. /* Commodore Amiga (CAMG) Viewmodes structure */
  51. typedef struct {
  52.    ULONG ViewModes;
  53.    } CamgChunk;
  54.  
  55. #define ID_ILBM         MAKE_ID('I','L','B','M')
  56. #define ID_BMHD         MAKE_ID('B','M','H','D')
  57. #define ID_CMAP         MAKE_ID('C','M','A','P')
  58. #define ID_CAMG         MAKE_ID('C','A','M','G')
  59. #define ID_BODY         MAKE_ID('B','O','D','Y')
  60.  
  61. void PrintILBMInfo(struct IFFHandle *);
  62.  
  63. #define MINARGS 2
  64.  
  65. /* 2.0 Version string for c:Version to find */
  66. UBYTE vers[] = "\0$VER: ilbmscan 37.3";
  67.  
  68. UBYTE usage[] = "Usage: ilbmscan IFFfilename (or -c for clipboard)";
  69.  
  70. /*
  71.  * Text error messages for possible IFFERR_#? returns from various
  72.  * IFF routines.  To get the index into this array, take your IFFERR code,
  73.  * negate it, and subtract one.
  74.  *  idx = -error - 1;
  75.  */
  76. char    *errormsgs[] = {
  77.     "End of file (not an error).",
  78.     "End of context (not an error).",
  79.     "No lexical scope.",
  80.     "Insufficient memory.",
  81.     "Stream read error.",
  82.     "Stream write error.",
  83.     "Stream seek error.",
  84.     "File is corrupt.",
  85.     "IFF syntax error.",
  86.     "Not an IFF file.",
  87.     "Required call-back hook missing.",
  88.     "Return to client.  You should never see this."
  89. };
  90.  
  91. struct Library *IFFParseBase;
  92.  
  93.  
  94. void main(int argc, char **argv)
  95. {
  96.     struct IFFHandle    *iff = NULL;
  97.     long        error;
  98.     short        cbio;
  99.  
  100.         /* if not enough args or '?', print usage */
  101.         if(((argc)&&(argc<MINARGS))||(argv[argc-1][0]=='?'))
  102.         {
  103.             printf("%s\n",usage);
  104.             exit(RETURN_OK);
  105.             }
  106.  
  107.     /*
  108.      * Check to see if we are doing I/O to the Clipboard.
  109.      */
  110.     cbio = (argv[1][0] == '-'  &&  argv[1][1] == 'c');
  111.  
  112.     if (!(IFFParseBase = OpenLibrary ("iffparse.library", 0L)))
  113.         {
  114.         printf("Can't open iff parsing library.");
  115.         goto bye;
  116.         }
  117.  
  118.     /*
  119.      * Allocate IFF_File structure.
  120.      */
  121.     if (!(iff = AllocIFF ()))
  122.         {
  123.         printf ("AllocIFF() failed.");
  124.         goto bye;
  125.         }
  126.  
  127.     /*
  128.      * Internal support is provided for both AmigaDOS files, and the
  129.      * clipboard.device.  This bizarre 'if' statement performs the
  130.      * appropriate machinations for each case.
  131.      */
  132.     if (cbio)
  133.         {
  134.         /*
  135.          * Set up IFF_File for Clipboard I/O.
  136.          */
  137.         if (!(iff->iff_Stream =
  138.                 (ULONG) OpenClipboard (PRIMARY_CLIP)))
  139.             {
  140.             printf("Clipboard open failed.");
  141.             goto bye;
  142.             }
  143.         InitIFFasClip (iff);
  144.         }
  145.     else
  146.         {
  147.         /*
  148.          * Set up IFF_File for AmigaDOS I/O.
  149.          */
  150.         if (!(iff->iff_Stream = Open (argv[1], MODE_OLDFILE)))
  151.             {
  152.             printf("File open failed.");
  153.             goto bye;
  154.             }
  155.         InitIFFasDOS (iff);
  156.         }
  157.  
  158.     /*
  159.      * Start the IFF transaction.
  160.      */
  161.     if (error = OpenIFF (iff, IFFF_READ))
  162.         {
  163.         printf("OpenIFF failed.");
  164.         goto bye;
  165.         }
  166.  
  167.     /* We want to collect BMHD and CAMG */
  168.     PropChunk(iff, ID_ILBM, ID_BMHD);
  169.     PropChunk(iff, ID_ILBM, ID_CAMG);
  170.     PropChunk(iff, ID_ILBM, ID_CMAP);
  171.  
  172.     /* Stop at the BODY */
  173.     StopChunk(iff, ID_ILBM, ID_BODY);
  174.  
  175.     /* And let us know (IFFERR_EOC) when leaving a FORM ILBM */
  176.     StopOnExit(iff,ID_ILBM, ID_FORM);
  177.  
  178.     /* Do the scan.
  179.      * The while(1) will let us delve into more complex formats
  180.      * to find FORM ILBM's
  181.      */
  182.     while (1)
  183.         {
  184.         error = ParseIFF(iff, IFFPARSE_SCAN);
  185.         /*
  186.          * Since we're only interested in when we enter a context,
  187.          * we "discard" end-of-context (_EOC) events.
  188.          */
  189.         if (error == IFFERR_EOC)
  190.             {
  191.             printf("Exiting FORM ILBM\n\n");
  192.             continue;
  193.             }
  194.         else if (error)
  195.             /*
  196.              * Leave the loop if there is any other error.
  197.              */
  198.             break;
  199.  
  200.         /*
  201.          * If we get here, error was zero
  202.          * Since we did IFFPARSE_SCAN, zero error should mean
  203.          * we are at our Stop Chunk (BODY)
  204.          */
  205.         PrintILBMInfo(iff);
  206.         }
  207.  
  208.     /*
  209.      * If error was IFFERR_EOF, then the parser encountered the end of
  210.      * the file without problems.  Otherwise, we print a diagnostic.
  211.      */
  212.     if (error == IFFERR_EOF)
  213.         printf("File scan complete.\n");
  214.     else
  215.         printf("File scan aborted, error %ld: %s\n",
  216.             error, errormsgs[-error - 1]);
  217.  
  218. bye:
  219.     if (iff) {
  220.         /*
  221.          * Terminate the IFF transaction with the stream.  Free
  222.          * all associated structures.
  223.          */
  224.         CloseIFF (iff);
  225.  
  226.         /*
  227.          * Close the stream itself.
  228.          */
  229.         if (iff->iff_Stream)
  230.             if (cbio)
  231.                 CloseClipboard ((struct ClipboardHandle *)
  232.                         iff->iff_Stream);
  233.             else
  234.                 Close (iff->iff_Stream);
  235.  
  236.         /*
  237.          * Free the IFF_File structure itself.
  238.          */
  239.         FreeIFF (iff);
  240.         }
  241.     if (IFFParseBase)    CloseLibrary (IFFParseBase);
  242.  
  243.     exit (RETURN_OK);
  244. }
  245.  
  246.  
  247. void
  248. PrintILBMInfo(iff)
  249. struct IFFHandle *iff;
  250. {
  251.     struct StoredProperty    *sp;
  252.     BitMapHeader     *bmhd;
  253.     CamgChunk    *camg;
  254.  
  255.     /*
  256.      * Get a pointer to the stored propery BMHD
  257.      */
  258.     if (!(sp = FindProp(iff, ID_ILBM, ID_BMHD)))
  259.         printf("No BMHD found\n");
  260.     else
  261.         {
  262.         /* If property is BMHD, sp->sp_Data is ptr to data in BMHD */
  263.         bmhd = (BitMapHeader *)sp->sp_Data;
  264.         printf("BMHD: Width      = %ld\n",bmhd->w);
  265.         printf("      Height     = %ld\n",bmhd->h);
  266.         printf("      PageWidth  = %ld\n",bmhd->PageWidth);
  267.         printf("      PageHeight = %ld\n",bmhd->PageHeight);
  268.         printf("      nplanes    = %ld\n",bmhd->nplanes);
  269.                printf("      Masking    = %ld\n",bmhd->Masking);
  270.                 printf("      Compression= %ld\n",bmhd->Compression);
  271.         printf("      TransColor = %ld\n",bmhd->TransparentColor);
  272.         printf("      X/Y Aspect = %ld/%ld\n",bmhd->XAspect,bmhd->YAspect);
  273.         }
  274.  
  275.     /*
  276.      * Get a pointer to the stored propery CMAP
  277.      */
  278.     if (!(sp = FindProp(iff, ID_ILBM, ID_CMAP)))
  279.         printf("No CMAP found\n");
  280.     else
  281.         {
  282.         /* If property is CMAP, sp->sp_Data is ptr to data in CMAP */
  283.         printf("CMAP: contains RGB values for %ld registers\n",
  284.                 sp->sp_Size / 3);
  285.         }
  286.  
  287.     /*
  288.      * Get a pointer to the stored propery CAMG
  289.      */
  290.     if (!(sp = FindProp(iff, ID_ILBM, ID_CAMG)))
  291.         printf("No CAMG found\n");
  292.     else
  293.         {
  294.         /* If property is CAMG, sp->sp_Data is ptr to data in CAMG */
  295.         camg = (CamgChunk *)sp->sp_Data;
  296.         printf("CAMG: ModeID     = $%08lx\n\n",camg->ViewModes);
  297.         }
  298.     
  299. }
  300.  
  301.